package cn.mcsys.ticket.service.timer;

import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.viewer.entity.AirTickets;
import cn.mcsys.ticket.dao.AirShowPWDAO;
import cn.mcsys.ticket.entity.AirshowPW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TicketsTimer {

    @Autowired
    private BackService backService;

    @Autowired
    private AirShowPWDAO airShowPWDAO;


    public void saveTickets(){
        int pageNO = 0;
        System.out.println("进入票务定时器------------------------------使用的线程是:"+Thread.currentThread().getName());
        while (true) {
            List<AirshowPW> airshowPWS = airShowPWDAO.selectAll(pageNO);
            if(airshowPWS.size() == 0){
                break;
            }
            pageNO += airshowPWS.size();
            System.out.println("pageNO:"+pageNO);
//            List<AirTickets> airTicketsList = new ArrayList<>();
            for (AirshowPW airshowPW : airshowPWS) {
                AirTickets airTickets = new AirTickets();
                String performname = airshowPW.getPerformname();
                String phone = airshowPW.getPhone();
                String productName = airshowPW.getProductName();
                switch (productName) {
                    case "个人票（证件票）":
                        airTickets.setParType(0);
                        break;
                    case "长者学生儿童票（证件票）":
                        airTickets.setParType(1);
                        break;
                    case "个人票（实物票）":
                        airTickets.setParType(2);
                        break;
                    case "长者学生儿童票（实物票）":
                        airTickets.setParType(3);
                        break;
                    case "团队票":
                        airTickets.setParType(4);
                        break;
                    case "个人票+动态演示区套票":
                        airTickets.setParType(5);
                        break;
                    case "长者学生儿童票+动态演示区套票":
                        airTickets.setParType(6);
                }
                String idcardType = airshowPW.getIdcardType();
                switch (idcardType) {
                    case "身份证":
                        airTickets.setTicketType(0);
                        break;
                    case "回乡证":
                        airTickets.setTicketType(1);
                        break;
                    case "台胞证":
                        airTickets.setTicketType(2);
                        break;
                    case "外国人护照":
                        airTickets.setTicketType(3);
                        break;
                    case "实物票":
                        airTickets.setTicketType(4);
                }
                String cardID = airshowPW.getCardID();
                if(airTickets.getTicketType() == 4){
                    cardID = airshowPW.getCarcode();
                }
                airTickets.setPhone(phone);
                airTickets.setTicketNumber(cardID);
                airTickets.setTicketDate(performname);
                airTickets.setCreateDate(new Date());
                airTickets.setStatus(0);
                AirTickets alreadyTicket = backService.selectByAirTickets(airTickets);
                if (alreadyTicket == null) {
                    backService.saveTicket(airTickets);
                    System.out.println("保存票务信息");
                }
            }
        }
    }
}
